using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ISO
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region CONSTANTS
        int WINDOW_HEIGHT = 480;
        int WINDOW_WIDTH = 960;
        #endregion

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D _player_texture;
        Texture2D _enemy_texture;
        Texture2D _tile_texture;
        Texture2D _main_menu_texture;
        Texture2D _mouse_texture;

        Camera _camera1;

        List<Character> _characters;
        Player _player1;
        NonPlayer _enemy1;

        bool _clickable = true;

        enum Menus
        { Logo, Main, Game, GameOver, Map, Options, Audio, Video, Controls, Credits, Paused, Editor };

        enum Tiles
        { Template = 0, Grass = 1, Stone = 2};

        Menus _menu;
        Menus _previous;

        Map _current_map;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            _menu = Menus.Main;

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //Set the screen height and width
            graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
            graphics.PreferredBackBufferWidth = WINDOW_WIDTH;

            //Apply the changes made to the device
            graphics.ApplyChanges();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            _player_texture = this.Content.Load<Texture2D>("Player");
            _tile_texture = this.Content.Load<Texture2D>("Tile_Spritesheet");
            _enemy_texture = this.Content.Load<Texture2D>("Enemy");
            _mouse_texture = this.Content.Load<Texture2D>("Mouse");

            _main_menu_texture = this.Content.Load<Texture2D>("Main_Menu");
            _current_map = new Map();
            _characters = new List<Character>();
            _player1 = new Player(35, 35, _player_texture);
            _enemy1 = new NonPlayer(35, 40, _enemy_texture);
            _characters.Add(_player1);
            _characters.Add(_enemy1);

            _camera1 = new Camera(spriteBatch, _player1, _current_map);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            //controls the various menus of the game
            switch (_menu)
            {
                #region Main Menu
                case (Menus.Main):
                    if (_clickable)
                    {
                        if (Mouse.GetState().LeftButton == ButtonState.Pressed)
                        {
                            _clickable = false;
                        }
                        if (Mouse.GetState().X > 300 && Mouse.GetState().X < 630)
                        {
                            if (Mouse.GetState().Y >= 200 && Mouse.GetState().Y <= 240)
                            {
                                if (!_clickable)
                                {
                                    //MediaPlayer.Stop();
                                    //_currentSong = this.Content.Load<Song>("Area1\\ChurchMaster_Track1");
                                    //MediaPlayer.Play(_currentSong);
                                    _menu = Menus.Game;
                                    //NewGame();
                                    gameTime = new GameTime();
                                }
                            }
                            else if(Mouse.GetState().Y >= 300 && Mouse.GetState().Y <= 350)
                            {
                                if (!_clickable)
                                {
                                    _menu = Menus.Editor;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (Mouse.GetState().LeftButton == ButtonState.Pressed)
                            _clickable = true;
                    }
                    //_menu = Menus.Game;
                    break;
                #endregion
                #region Editor Menu
                case (Menus.Editor):
                    //if()
                    break;
                #endregion
                #region Main Game
                case (Menus.Game):
                    _player1.Move(_current_map);
                    /*_player1.Move(_current_map.TileVertices, 
                        _current_map.Tiles[_current_map.CurrentTiles[0] + (int)_player1.X_Index,
                        _current_map.CurrentTiles[1] + (int)_player1.Y_Index].Width,
                        _current_map.Tiles[_current_map.CurrentTiles[0] + (int)_player1.X_Index,
                        _current_map.CurrentTiles[1] + (int)_player1.Y_Index].Height);
                    _current_map.UpdateMap((int)_player1.X_Index, (int)_player1.Y_Index);*/
                    break;
                #endregion
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            GraphicsDevice.Clear(Color.CornflowerBlue);
            switch (_menu)
            {
                #region Main Menu
                case (Menus.Main):
                    IsMouseVisible = true;
                    spriteBatch.Draw(_main_menu_texture, new Rectangle(0, 0, 960, 480), Color.White);
                    break;
                #endregion

                #region Main Game
                case (Menus.Game):
                    _camera1.DrawMap(_tile_texture);
                    for(int x =0; x<_characters.Count;x++){
                        _camera1.DrawCharacter(_characters[x]);
                    }
                    _camera1.DrawMouse(_mouse_texture, Mouse.GetState().X, Mouse.GetState().Y);
                    break;
                #endregion
               
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }

        /// <summary>
        /// Draws the player
        /// </summary>
        
    }


}
